//network统一出口
import ZLRequest from "./request";
import { BASE_URL, TIME_OUT } from "./request/config";
import { ElMessage } from "element-plus";
import Cookies from "js-cookie";

const zlRequest = new ZLRequest({
  baseURL: BASE_URL,
  timeout: TIME_OUT,
  interceptors: {
    requestInterceptor: (config: any) => {
      //携带token的拦截
      const token = Cookies.get("token");
      if (token) {
        config.headers.Authorization = `Bearer ${token}`;
      }
      return config;
    },
    requestInterceptorCatch: (err) => {
      return err;
    },
    responseInterceptor: (res) => {
      return res;
    },
    responseInterceptorCatch: (err) => {
      ElMessage.error({
        type: err,
        center: true,
        message: err.response.data
      });
      // localCache.setCache("err", err.response);
      return err;
    }
  }
});
export { zlRequest };
